package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.service.ZJCAddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;



@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;
    @Reference
    private ZJCAddressService ZJCAddressService;

    @RequestMapping("/findAllPoint")
    public Result findAllPoint() {
        try {
            List<Address> addressList = addressService.findAllPoint();
            return new Result(true, "获取所有点成功", addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取所有点失败");
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return addressService.findPage(queryPageBean);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            addressService.deleteById(id);
            return new Result(true, "删除地址成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除地址失败！");
        }
    }


    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        System.out.println(address.getLongitude());
        try {
            addressService.add(address);
            return new Result(true, "添加地址成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加地址失败！");
        }
    }

    //根据id查询检查项
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Address address = addressService.findById(id);//发送请求
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address) {
        try {
            addressService.edit(address);//发送请求
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findByCondition")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = ZJCAddressService.findByConditon(queryPageBean);
        return pageResult;
    }

}
